﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ODP.Model;
using System.Net.Json;

namespace ODP.Web
{
    /// <summary>
    /// HomePage1 的摘要说明
    /// </summary>
    public class HomePage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.QueryString["action"];

            if (!string.IsNullOrEmpty(action))
            {
                action = action.ToLower();
            }

            switch (action)
            {
                case "gethomepage":
                    GetHomePage(context);
                    break;
                case "forward":
                    Forward(context);
                    break;
                case "getotheruserinfo":
                    GetOtherUserInfo(context);
                    break;
                case "mention":
                    Mention(context);
                    break;
                case "weiboadd":
                    if (string.IsNullOrEmpty(context.Request.Form["filepaths"]))
                    {
                        WeiBoAdd(context);
                    }
                    else
                    {
                        WeiBoAddWithPic(context);
                    }
                    break;
            }
        }

        #region 发表带图片的微博信息
        /// <summary>
        /// 发表带图片的微博信息
        /// </summary>
        /// <param name="context"></param>
        private void WeiBoAddWithPic(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();

            QueryStringParameter content = new QueryStringParameter("content", string.IsNullOrEmpty(context.Request.QueryString["content"]) ? "" : context.Request.QueryString["content"]);
            QueryStringParameter clientip = new QueryStringParameter("clientip", context.Request.UserHostAddress);
            QueryStringParameter jing = new QueryStringParameter("jing", "");
            QueryStringParameter wei = new QueryStringParameter("wei", "");

            List<QueryStringParameter> files = new List<QueryStringParameter>();

            if (!string.IsNullOrEmpty(context.Request.Form["filepaths"]))
            {
                string[] filepaths = context.Request.Form["filepaths"].Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string filepath in filepaths)
                {
                    files.Add(new QueryStringParameter("pic",filepath));
                }
            }

            string result = qqapi.WeiBoAddWithPic(ODP.Com.Current.OAuth, ODP.Com.Current.tType,files, new QueryStringParameter[] { content, clientip, jing, wei });

            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        #region 发表微博
        /// <summary>
        /// 发表微博
        /// </summary>
        /// <param name="context"></param>
        private void WeiBoAdd(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();

            QueryStringParameter content = new QueryStringParameter("content", string.IsNullOrEmpty(context.Request.QueryString["content"]) ? "" : context.Request.QueryString["content"]);
            QueryStringParameter clientip = new QueryStringParameter("clientip", context.Request.UserHostAddress);
            QueryStringParameter jing = new QueryStringParameter("jing", "");
            QueryStringParameter wei = new QueryStringParameter("wei", "");

            string result = qqapi.WeiBoAdd(ODP.Com.Current.OAuth, ODP.Com.Current.tType, new QueryStringParameter[] { content, clientip, jing, wei });
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        #region 发表评论
        /// <summary>
        /// 发表评论
        /// </summary>
        /// <param name="context"></param>
        private void Mention(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();

            QueryStringParameter content = new QueryStringParameter("content", string.IsNullOrEmpty(context.Request.QueryString["content"]) ? "" : context.Request.QueryString["content"]);
            QueryStringParameter clientip = new QueryStringParameter("clientip", context.Request.UserHostAddress);
            QueryStringParameter jing = new QueryStringParameter("jing", "");
            QueryStringParameter wei = new QueryStringParameter("wei", "");
            QueryStringParameter reid = new QueryStringParameter("reid", string.IsNullOrEmpty(context.Request.QueryString["reid"]) ? "" : context.Request.QueryString["reid"]);

            string result = qqapi.Mention(ODP.Com.Current.OAuth, ODP.Com.Current.tType, new QueryStringParameter[] { content, clientip, jing, wei, reid });
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        #region 获取其它微博用户的信息
        /// <summary>
        /// 获取其它微博用户的信息
        /// </summary>
        /// <param name="context"></param>
        private void GetOtherUserInfo(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();
            QueryStringParameter name = new QueryStringParameter("name", string.IsNullOrEmpty(context.Request.QueryString["username"]) ? "" : context.Request.QueryString["username"]);
            QueryStringParameter fopenid = new QueryStringParameter("fopenid", "");
            string result = qqapi.GetOtherUserInfo(ODP.Com.Current.OAuth, ODP.Com.Current.tType, new QueryStringParameter[] { name, fopenid });
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        #region 转发
        /// <summary>
        /// 转发
        /// </summary>
        /// <param name="context"></param>
        private void GetHomePage(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();
            QueryStringParameter pageflag = new QueryStringParameter("pageflag", string.IsNullOrEmpty(context.Request.QueryString["pageflag"]) ? "0" : context.Request.QueryString["pageflag"]);
            QueryStringParameter pagetime = new QueryStringParameter("pagetime", string.IsNullOrEmpty(context.Request.QueryString["pagetime"]) ? "0" : context.Request.QueryString["pagetime"]);
            QueryStringParameter reqnum = new QueryStringParameter("reqnum", "20");
            string result = qqapi.GetHomePage(ODP.Com.Current.OAuth, ODP.Com.Current.tType, new QueryStringParameter[] { pageflag, pagetime, reqnum });
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        #region 获取首页上的所有信息
        /// <summary>
        /// 获取首页上的所有信息
        /// </summary>
        /// <param name="context"></param>
        public void Forward(HttpContext context)
        {
            QQAPI.QQAPI qqapi = new QQAPI.QQAPI();

            QueryStringParameter content = new QueryStringParameter("content", string.IsNullOrEmpty(context.Request.QueryString["content"]) ? "" : context.Request.QueryString["content"]);
            QueryStringParameter clientip = new QueryStringParameter("clientip", context.Request.UserHostAddress);
            QueryStringParameter jing = new QueryStringParameter("jing", "");
            QueryStringParameter wei = new QueryStringParameter("wei", "");
            QueryStringParameter reid = new QueryStringParameter("reid", string.IsNullOrEmpty(context.Request.QueryString["reid"]) ? "" : context.Request.QueryString["reid"]);

            string result = qqapi.Forward(ODP.Com.Current.OAuth, ODP.Com.Current.tType, new QueryStringParameter[] { content, clientip, jing, wei, reid });
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}